package top.lisicheng.simple;

import com.rabbitmq.client.*;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author Lisicheng
 */
@Slf4j
public class Consumer {

    public static void main(String[] args) {

        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("123456");
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);

        try (Connection connection = connectionFactory.newConnection("消费者")) {

            Channel channel = connection.createChannel();

            String queueName = "queue2";
            channel.basicConsume(
                    queueName,
                    true,
                    (consumerTag, message) -> {
                        log.info("deliverCallback：{}，{}", consumerTag, message);
                        log.info("body：{}", new String(message.getBody()));
                    },
                    consumerTag -> {
                        log.info("cancelCallback：{}", consumerTag);
                    }
            );
            log.info("ok???");

        } catch (IOException | TimeoutException e) {
            log.info("error consumer rabbitmq", e);
        }

    }

}
